import type {  Datum, EnterpriseDetail, EnterpriseListData, EnterpriseListParams, EnterpriseParams, Industry, PutParkEnterpriseParams, RentBuilding, RentParams } from "@/types/enterprise"
import service, { request } from "@/utils/reuqest"

/**
 * 获取企业列表
 * @param { page, pageSize} params
 * @returns
 */
export const getEnterpriseListAPI = (params: EnterpriseListParams) => {
  return request<EnterpriseListData>('/park/enterprise', 'GET', params)
}

/**
 * 获取行业列表
 * @returns
 */
export const getIndustryListAPI = () => {
  return request<Industry[]>('/park/industry')
}

/**
 * 添加企业
 * @param {*} data
 * @returns
 */
export const createEnterpriseAPI = (data: EnterpriseParams) =>{
  return request('/park/enterprise', 'POST', data)
}

/**
 * 查看企业
 * @param id 
 * @returns 
 */
export const getParkEnterpriseAPI = (id: string) => {
  return request<PutParkEnterpriseParams>(`/park/enterprise/${id}`,'GET')
}

/**
 * 园区管理-编辑企业
 */

export const putParkEnterpriseAPI = (data:Partial<PutParkEnterpriseParams>) => {
  return request('/park/enterprise', 'PUT',data)
}

/**
 * 删除企业
 * @param {*} id
 * @returns
 */
export const delEnterpriseAPI = (id: string) => {
  return request(`/park/enterprise/${id}`, 'DELETE')
}

/**
 * 获取空置中楼宇列表（未租赁）
 * @param {*}
 * @returns
 */
export const getRentBuildListAPI = () => {
  return request<RentBuilding[]>('/park/rent/building')
}

/**
 * 给当前企业添加合同
 * @param {*} data
 * @returns
 */
export const  createRentAPI = (data: RentParams) => {
  return request('/park/enterprise/rent', 'POST', data)
}


/**
 * 获取企业下合同列表
 * @param {*} id
 * @returns
 */
export const getRentListAPI = (id: number) => {
  return request<Datum[]>(`/park/enterprise/rent/${id}`)
}

/**
 * 退租
 * @param {合同id} rentId
 * @returns
 */
export const outRentAPI = (rentId: string) => {
  return request(`/park/enterprise/rent/${rentId}`, 'PUT')
}

/**
 * 删除合同
 * @param {合同id} rentId
 * @returns
 */
export const deleteRentAPI = (rentId: string) => {
  return request(`/park/enterprise/rent/${rentId}`, 'DELETE')
}

/**
 * 获取企业详情
 * @param {*} id
 * @returns
 */
export const getEnterpriseDetailAPI = (id: string) => {
  return request<EnterpriseDetail>(`/park/enterprise/${id}`)
}

/**
 * 下载合同
 * @param {*} id
 * @returns
 */
export const downloadContract = (id: number) => {
  // ❌ 普通的写法，解析成字符串是错误的
  // return request(`/download/${id}`)

  // ✅ 第二个范型参数为 Blob，请求配置中设置 responseType: 'blob'
  return service.request<null, Blob>({
    method: 'GET',
    url: `/download/${id}`,
    responseType: 'blob'  // ✅ 把二进制数据流转换成 blob 对象，用于下载
  })
}